//
//  HMVisitorView.swift
//  Weibo
//
//  Created by 京城 on 16/7/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

protocol visitorViewDelegate : NSObjectProtocol {
    
    func visitorViewDidRegister()
    func visitorViewDidLogin()
    
}

class HMVisitorView: UIView {

    weak var delegate:visitorViewDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")

    }
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)

        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        self.setupUI()
        
    }
    
    //通过图片名字  和 label 返回不同的visitor视图
    func setupVistorInfo(name: String ,title:String){
        
        houseView.hidden = true
        self.sendSubviewToBack(coverView)
        iconView.image = UIImage(named: name)
        messageLabel.text = title
        
        
    }
    
    /// 图片旋转 
    func imgeRotate() {
        
        let animation =  CABasicAnimation(keyPath: "transform.rotation")
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.toValue = 2 * M_PI
        animation.removedOnCompletion = false
        self.iconView.layer.addAnimation(animation, forKey: "transform")
    }
    
    func setupUI() {
        
        self .addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(houseView)
        self.addSubview(messageLabel)
        self.addSubview(registerButton)
        self.addSubview(loginButton)

        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        //图片约束
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -20))
        //遮罩的约束

       self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
       self.addConstraint( NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 房子 CenterX
        // 往父控件添加约束
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 房子 CenterY
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 消息label CenterX
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 消息label Top到iconView底部
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 消息label 宽度
        // 当 toItem = nil, attribute = NotAnAttribute
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 260))
        
        // 注册按钮
        // 按照文字的书写顺序: Leading, Trailing
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
       
        
        
        // 登录按钮
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))

      
        
    }
    
    //注册按钮点击
   @objc private func registerClick() {
    
        print("registerClick")
    delegate?.visitorViewDidRegister()
    }
    //登录按钮点击
   @objc private func loginClick() {
        
        print("loginClick")
    delegate?.visitorViewDidLogin()
        
    }
    //图片
   private  lazy var iconView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var coverView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //小房子
   private lazy var houseView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //label
   private lazy var messageLabel:UILabel = {
       
        let label = UILabel(frame: CGRectZero)
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 0
        label.text = "解决洛杉矶劳动节快乐就是了看到据了解"
        label.sizeToFit()
         return label
    }()
    //注册按钮
  private  lazy var registerButton :UIButton = {
        
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        
    }()
    //登录按钮
  private  lazy var loginButton :UIButton = {
        
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
       btn.titleLabel?.font = UIFont.systemFontOfSize(18)
       btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    
        
    }()

    
    
}
